local jiudian = fk.CreateSkill{
  name = "xiaobai__jiudian",
}

local XBB = require "packages.xiaobai.util"

jiudian:addEffect("viewas",{
  pattern = ".",
  interaction = function (self, player)
    local cardNames = Fk:getAllCardNames("bt", true)
    local able_name = player:getViewAsCardNames(jiudian.name, cardNames, {}, player:getTableMark("@$xiaobai__jiudian-round"))
    return UI.CardNameBox{
      choices = able_name,
      all_choices = cardNames,
    }
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = jiudian.name
    return card
  end,
  before_use = function (self, player, use)
    local txdbs = table.filter(player:getCardIds("h"), function (id)
      return XBB.startsWithEndsWith(Fk:getCardById(id).name)
    end)
    player.room:recastCard(txdbs, player, jiudian.name)
  end,
  enabled_at_play = function (self, player)
    local cardNames = Fk:getAllCardNames("bt", true)
    local able_name = player:getViewAsCardNames(jiudian.name, cardNames, {}, player:getTableMark("@$xiaobai__jiudian-round"))
    return #able_name > 0 and table.find(player:getCardIds("h"), function (id)
      return XBB.startsWithEndsWith(Fk:getCardById(id).name)
    end)
  end,
  enabled_at_response = function (self, player, response)
    local cardNames = Fk:getAllCardNames("bt", true)
    local able_name = player:getViewAsCardNames(jiudian.name, cardNames, {}, player:getTableMark("@$xiaobai__jiudian-round"))
    return #able_name > 0 and not response and table.find(player:getCardIds("h"), function (id)
      return XBB.startsWithEndsWith(Fk:getCardById(id).name) end)
  end
})

jiudian:addEffect(fk.CardUseFinished, {
  mute = true,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(jiudian.name)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMarkIfNeed(player, "@$xiaobai__jiudian-round", data.card.trueName)
  end
})

jiudian:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  for _, p in ipairs(room.alive_players) do
    if p ~= player and not p:hasSkill("xiaobai__jiudian&") then
      room:handleAddLoseSkills(p, "xiaobai__jiudian&", nil, false, true)
    end
  end
  room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
    room:addTableMarkIfNeed(player, "@$xiaobai__jiudian-round", e.data.card.trueName)
  end, Player.HistoryRound)
end)

jiudian:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@$xiaobai__jiudian-round", 0)
  if not table.find(room.alive_players, function (p)
    return p:hasSkill("xiaobai__jiudian")
  end) then
    return
  end
  for _, p in ipairs(room.alive_players) do
    if p ~= player and p:hasSkill("xiaobai__jiudian&") then
      room:handleAddLoseSkills(p, "-xiaobai__jiudian&", nil, false, true)
    end
  end
end)

Fk:loadTranslationTable{
  ["xiaobai__jiudian"] = "究典",
  [":xiaobai__jiudian"] = "每回合限一次，其他角色可以赠予你一张“小白杯”武将，然后摸一张牌；"..
    "你可以重铸所有“小白杯”武将以视为使用一张本轮没有角色使用过的基本牌或普通锦囊牌。",

  ["@$xiaobai__jiudian-round"] = "究典",
  ["xiaobai__jiudian&"] = "究典",
  [":xiaobai__jiudian&"] = "",
  [":xiaobai__jiudian_other"] = "每回合限一次，你可以赠予 {1} 一张“小白杯”武将，然后摸一张牌。"
}
--- 获得玩家名字
---@param room AbstractRoom
---@param pid integer
---@param color string?
---@return string
local function getPlayerStr(room, pid, color)
  if not color then color = "white" end
  if not pid then
    return ""
  end
  local p = room:getPlayerById(pid)
  local str = '<font color="%s"><b>%s</b></font>'
  if p.general == "anjiang" and (p.deputyGeneral == "anjiang"
    or not p.deputyGeneral) then
    local ret = Fk:translate("seat#" .. p.seat)
    if pid == Self.id then
      ret = ret .. Fk:translate("playerstr_self")
    end
    return string.format(str, color, ret)
  end
  local ret = p.general
  ret = Fk:translate(ret)
  if p.deputyGeneral and p.deputyGeneral ~= "" then
    ret = ret .. "/" .. Fk:translate(p.deputyGeneral)
  end
  for _, p2 in ipairs(Fk:currentRoom().players) do
    if p2 ~= p and p2.general == p.general and p2.deputyGeneral == p.deputyGeneral then
      ret = ret .. ("[%d]"):format(p.seat)
      break
    end
  end
  if pid == Self.id then
    ret = ret .. Fk:translate("playerstr_self")
  end
  ret = string.format(str, color, ret)
  return ret
end

local jiudian_other = fk.CreateSkill{
  name = "xiaobai__jiudian&",
  dynamic_desc =function (self, player, lang)
    local owners = table.filter(Fk:currentRoom().players, function (p)
      return p:isAlive() and p:hasSkill(jiudian.name)
    end)
    print(#owners)
    if #owners > 0 then
      return "xiaobai__jiudian_other:"..table.concat(table.map(owners, function (p)
        return getPlayerStr(Fk:currentRoom(), p.id)
      end), "、")
    else
      return "xiaobai__jiudian&"
    end
  end
}

jiudian_other:addEffect("active", {
  can_use = function (self, player)
    return player:usedSkillTimes(jiudian_other.name) == 0
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0 and to_select:hasSkill(jiudian.name) and to_select ~= player
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected == 0 and XBB.startsWithEndsWith(Fk:getCardById(to_select).name)
  end,
  target_num = 1,
  card_num = 1,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local card = effect.cards[1]
    room:obtainCard(target, card, true, fk.ReasonGive, player, jiudian.name)
    room:drawCards(player, 1, jiudian.name)
  end
})

return {jiudian, jiudian_other}